<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/11
 * Time: 9:13
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Card extends Common
{
    public function card()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['store_name']) && $pagedata['store_name'] != '') {
                $where['store_name'] = array('like', '%' . $pagedata['store_name'] . '%');
            }
            if (isset($pagedata['rename']) && $pagedata['rename'] != '') {
                $where['rename'] = array('like', '%' . $pagedata['rename'] . '%');
            }
            if (isset($pagedata['is_pass']) && $pagedata['is_pass'] != '') {
                $where['is_pass'] = array('like', '%' . $pagedata['is_pass'] . '%');
            }
            $count = Db::name('my_card')->where($where)->count();
            $data = Db::name('my_card')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            return view();
        }
    }

    //展示图片
    public function viewImage()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = Db::name('card_img')->field('image')->where('card_id', $post['id'])->select();

            if ($data) {
                foreach ($data as $key => $v) {
                    $res [] = $v;
                }
                return json(['code' => 0, 'data' => $res]);

            } else {
                return ['code' => 1, 'message' => '暂无图片'];
            }
        }
    }

    public function pass()
    {
        if (Request::instance()->isAjax()) {
            $post = input('param.');
            $res = Db::name('my_card')->where('id', $post['id'])->setField('is_pass', $post['is_pass']);
            if ($res) {
                if ($post['is_pass'] == 1) {
                    //修改用户信息
                    Db::name('user')->where('uid', $post['uid'])->setField('is_card', 1);
                    $send = [
                        '1' => $post['name'],
                        '2' => $post['zhiwei']
                    ];
                     sendJsmsMessage($post['phone'],177164,$send);
                     send_user_mes($post['uid'], '电子名片申请',"您申请的电子名片 店铺".$post['name']." 已通过审核");
                } else {
                    $send = [
                        '1' => '已',
                    ];
                     sendJsmsMessage($post['phone'],177168,$send);
                     send_user_mes( $post['uid'],"电子名片申请'","您申请的电子名片已拒绝,您可以重新在app内申请");
                }
                return 0;
            } else {
                return 1;
            }
        }
    }

    //职位关系
    public function label()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_display'] = array('eq', 0);
            if (isset($pagedata['name']) && $pagedata['name'] != '') {
                $where['name'] = array('like', '%' . $pagedata['name'] . '%');
            }
            $count = Db::name('card_zw')->where($where)->count();
            $data = Db::name('card_zw')->field("id,name")
                ->limit($startrow, $pagesize)
                ->where($where)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return $this->fetch();
        }
    }

    /*
     * 添加职位
     */
    public function addColumn()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            //检查用户名是否存在
            $check = Db::name('card_zw')->where("name", $post['name'])->find();
            if ($check) {
                return ['code' => '职位已经存在'];
                exit;
            }
            $data = [
                'name' => $post['name'],

            ];

            $res = Db::name('card_zw')->insert($data);

            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => '添加失败'];
            }
        }

    }

    /*
     * 修改职位
     */
    public function editColumn()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'name' => $post['data']['name'],
            );

            $where['name'] = array('eq', $post['data']['name']);
            //检查用户名是否存在
            $check = Db::name('card_zw')->where($where)->find();
            if ($check) {
                return ['code' => '职位关系已经存在'];
                exit;
            }

            $res = Db::name('card_zw')->where('id', $post['data']['id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 删除职位
     */
    public function delColumn()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('card_zw')->where('id', $post['id'])->delete();
            if ($res !== false) {
                return "1";
            } else {
                return "2";
            }
        }
    }


}